// 将本控制器函数作为结果返回给router.js

import util from "../common/util"

function duesCtrl($scope, $http, $state) {
    $scope.welcome_info = "会费管理";
    $scope.duesRecordDatas = [];
    $scope.pageItems = [5, 10, 20, 30, 50, 100];
    $scope.pageInfo = {
        pageNo: 1,
        pageSize: 20,
        totalCnt: 0,
        pageNum: 1
    };
    $scope.$emit('updateRoute', 'dues');

    function init() {
        initOrUpdateData();
    }

    function initOrUpdateData() {
        $http.get("/basketballfire/rest/dues/list", {params: $scope.pageInfo}).then(function (data) {
            console.log(data);
            $scope.duesRecordDatas = data.data.data;
            $scope.pageInfo.totalCnt = data.data.totalCnt;
            $scope.pageList = [];
            let pageNum = Math.ceil($scope.pageInfo.totalCnt / $scope.pageInfo.pageSize); //分页数
            for (let index = 0; index < pageNum; index++) {
                $scope.pageList.push(index + 1);
            }
            $scope.pageInfo.pageNum = pageNum;
            for (let i = 0; i < $scope.duesRecordDatas.length; i++) {
                let duesRecord = $scope.duesRecordDatas[i];
                duesRecord.lastUpdateTimeDesc = util.getFormatDate(duesRecord.lastUpdateTime);
            }
        });
    }

    $scope.deleteDues = function (dues) {
        let msg = "您真的确定要删除吗？";
        if (!confirm(msg)) {
            return;
        }
        if (dues.activityId !== "") {
            alert("活动消费不能直接删除！");
            return;
        }

        $http.delete('/basketballfire/rest/dues?id=' + dues.id).then(function () {
            console.log("success!");
            alert("删除成功！");
            initOrUpdateData();
        }, function () {
            console.log("failed!");
            alert("删除失败！");
        });

    };

    $scope.selectPage = function (pageNo, pageSize) {
        $scope.pageInfo.pageNo = pageNo;
        $scope.pageInfo.pageSize = pageSize;
        initOrUpdateData();
    };

    init();
}

export default duesCtrl;